// text组件实现
window.add_text = function(x, y, w, h, color, bgColor, borderRadius, title, hide, tag, callback, divid) {
    // 确保参数有默认值
    x = x || 100;
    y = y || 100;
    w = w || 200;
    h = h || 40;
    color = color || '#000000';
    bgColor = bgColor || false;
    borderRadius = borderRadius || 0;
    title = title || '文字标签';
    hide = hide || false;
    tag = tag || '';
    callback = callback || null;
    
    // 生成唯一ID
    const id = 'text_' + Date.now() + '_' + Math.floor(Math.random() * 10000);
    
    // 创建文字标签元素
    const text = document.createElement('div');
    text.id = id;
    text.textContent = title;
    
    // 设置样式
    text.style.position = 'absolute';
    text.style.left = x + 'px';
    text.style.top = y + 'px';
    text.style.width = w + 'px';
    text.style.height = h + 'px';
    text.style.color = color;
    text.style.lineHeight = h + 'px';
    text.style.textAlign = 'center';
    text.style.borderRadius = borderRadius + 'px';
    
    // 设置背景色
    if (bgColor) {
        text.style.backgroundColor = bgColor;
    } else {
        text.style.backgroundColor = 'transparent';
    }
    
    // 附加样式和标记
    if (hide) text.style.display = 'none';
    if (tag) text.setAttribute('data-tag', tag);
    
    // 事件处理
    text.onmousemove = function(e) { handleEvent('text', id, 0, title, tag, e.clientX, e.clientY, null, callback, divid); };
    text.onclick = function(e) { handleEvent('text', id, 1, title, tag, e.clientX, e.clientY, null, callback, divid); };
    text.oncontextmenu = function(e) { e.preventDefault(); handleEvent('text', id, 2, title, tag, e.clientX, e.clientY, null, callback, divid); return false; };
    text.onmouseover = function(e) { handleEvent('text', id, 5, title, tag, e.clientX, e.clientY, null, callback, divid); };
    text.onmouseout = function(e) { handleEvent('text', id, 6, title, tag, e.clientX, e.clientY, null, callback, divid); };
    text.onkeydown = function(e) { handleEvent('text', id, 3, title, tag, 0, 0, e.key, callback, divid); };
    text.onkeyup = function(e) { handleEvent('text', id, 4, title, tag, 0, 0, e.key, callback, divid); };
    
    // 添加到页面或指定父div
    const parent = divid ? document.getElementById(divid) : document.body;
    parent.appendChild(text);
    
    // 初始化texts存储空间
    if (!elementStore.texts) {
        elementStore.texts = {};
    }
    
    // 存储引用
    elementStore.texts[id] = text;
    
    return id;
}

window.del_text = function(id) {
    if (elementStore.texts[id]) {
        elementStore.texts[id].remove();
        delete elementStore.texts[id];
    }
}

window.upd_text = function(id, x, y, w, h, color, bgColor, borderRadius, title, hide) {
    const text = elementStore.texts[id];
    if (text) {
        if (x !== undefined) text.style.left = x + 'px';
        if (y !== undefined) text.style.top = y + 'px';
        if (w !== undefined) text.style.width = w + 'px';
        if (h !== undefined) text.style.height = h + 'px';
        if (color !== undefined) text.style.color = color;
        if (bgColor !== undefined) text.style.backgroundColor = bgColor || 'transparent';
        if (borderRadius !== undefined) text.style.borderRadius = borderRadius + 'px';
        if (title !== undefined) text.textContent = title;
        if (hide !== undefined) text.style.display = hide ? 'none' : 'block';
    }
}